package com.zfzs.post.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import com.zfzs.post.aop.log.Log;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.common.security.AuthorizationUser;
import com.zfzs.post.common.security.ImgResult;
import com.zfzs.post.common.utils.VerifyCodeUtils;
import com.zfzs.post.model.dto.LoginCodeDTO;
import com.zfzs.post.model.dto.RefreshTokenDTO;
import com.zfzs.post.service.RedisService;
import com.zfzs.post.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 授权、根据token获取用户详细信息
 *
 * @author ck
 * @date 2018-11-23
 */
@Slf4j
@RestController
@RequestMapping("auth")
public class AuthenticationController {

    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserService userService;

    /**
     * 登录授权
     *
     * @param authorizationUser
     * @return
     */
    @Log("密码登录账号")
    @PostMapping(value = "${jwt.auth.path}")
    public ResponseResult login(@Validated @RequestBody AuthorizationUser authorizationUser) {
        Map<String, String> map = userService.loginPWd(authorizationUser);
        return ResponseResult.ok(map);
    }

    /**
     * 验证码登录
     */
    @Log("手机验证码登录账号")
    @PostMapping(value = "login_sms_code")
    public ResponseResult loginSmsCode(@Validated @RequestBody LoginCodeDTO dto) {
        Map<String,String> map = userService.loginSmsCode(dto);
        return ResponseResult.ok("注册成功", map);
    }

    /**
     * 刷新token
     *
     * @param
     * @return
     */
    @Log("刷新Token")
    @PostMapping(value = "/refresh_token")
    public ResponseResult refreshToken(@Validated @RequestBody RefreshTokenDTO dto) {
        Map<String,String> map = userService.refreshToken(dto);
        return ResponseResult.ok(map);
    }

    /**
     * 账号退出
     */
    @Log("退出账号")
    @PostMapping(value = "/login_out")
    public ResponseResult loginOut(HttpServletRequest request) {
        userService.loginOut(request);
        return ResponseResult.okMsg("退出成功");
    }

    /**
     * 获取图片验证码
     */
    @PostMapping(value = "/vCode")
    public ResponseResult getCode() throws IOException {
        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        String uuid = IdUtil.simpleUUID();
        redisService.saveCode(uuid, verifyCode);
        // 生成图片
        int w = 111, h = 36;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("img",new ImgResult(Base64.encode(stream.toByteArray()), uuid));
            return ResponseResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            stream.close();
        }
    }
}
